
package com.example.xiaoweiassistant.Fragment;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;


import com.baidu.speech.EventListener;
import com.baidu.speech.EventManager;
import com.baidu.speech.EventManagerFactory;
import com.baidu.speech.asr.SpeechConstant;
import com.example.xiaoweiassistant.Activity.MineActivity.mineActivity;
import com.example.xiaoweiassistant.R;

import com.example.xiaoweiassistant.util;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.internal.Util;
import top.defaults.colorpicker.ColorPickerPopup;
import top.defaults.colorpicker.ColorPickerView;

public  class  indexFragment extends Fragment {



    private Context indexFragmentContext ;
    private SeekBar seekBar;
    private ImageView mineEnter;
    private TextView ligth_add;
    private EditText editText;
    private LinearLayout LED_LightControl;
    private RelativeLayout lightControl ;

    public boolean lightControlIsPress = false;
    private LinearLayout colorModeEnter;
    private LinearLayout colorModePanel;
    private ColorPickerView colorPicker;
    private LinearLayout brilliant;
    private  boolean startBrillant = false ;
    private  RelativeLayout all ;

    @Override
    public void onAttach( Activity activity) {
        super.onAttach(activity);







        indexFragmentContext = activity  ;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View inflate = getLayoutInflater().inflate(R.layout.fragment_index, null, false);
        init(inflate);
        EnterEvent();
        LightControlEvent();
        colorModeEvent();
        BrilliantEvent();
        return inflate ;
    }


    // 处理入口事件
    private void EnterEvent() {
        LED_LightControl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!lightControlIsPress){
                    Animation animation = AnimationUtils.loadAnimation(indexFragmentContext, R.anim.out);
                    animation.setRepeatMode(Animation.ZORDER_TOP);


                    lightControl.setAnimation(animation);
                    lightControl.setVisibility(View.VISIBLE);

                    lightControlIsPress = true ;
                }else {
                    Animation animation = AnimationUtils.loadAnimation(indexFragmentContext, R.anim.in);

                    lightControl.setVisibility(View.GONE);
                    lightControl.setAnimation(animation);
                    lightControlIsPress = false ;
                }

            }
        });
    }

    private void init(View root) {
        // 灯光控制
        seekBar = root.findViewById(R.id.lightControlSeekBar);
        mineEnter = root.findViewById(R.id.mineEnter);
        ligth_add = root.findViewById(R.id.ligth_add);
        editText = root.findViewById(R.id.lightControlEditText);
        LED_LightControl = root.findViewById(R.id.LED_LightControl);
        lightControl = root.findViewById(R.id.lightControl);
         all = root.findViewById(R.id.all);

        // 色彩模式
        colorModeEnter = root.findViewById(R.id.colorModeEnter);
        colorModePanel = root.findViewById(R.id.colorModePanel);
        colorPicker = root.findViewById(R.id.colorPicker);

        //流光溢彩

        brilliant = root.findViewById(R.id.Brilliant);


    }

    // 处理灯光控制
    private void LightControlEvent() {

        // 进入跳转
        mineEnter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(indexFragmentContext , mineActivity.class));
            }
        });



        // seekBar监听
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                util.startRequest("lightness" , progress , "LEDControl");
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        // ligth_add
        ligth_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String lightNess = editText.getText().toString();
                if(!lightNess.equals("")|| lightNess != null){
                    util.startRequest("lightness" , Integer.parseInt(lightNess)  , "LEDControl");
                    seekBar.setProgress(Integer.parseInt(lightNess));
                }

            }
        });

    }

    // 处理色彩模式
    private void colorModeEvent() {

        colorModeEnter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ColorPickerPopup.Builder builder = new ColorPickerPopup.Builder(indexFragmentContext)
                        .okTitle("确定")
                        .cancelTitle("取消")
                        .enableBrightness(true)
                        .enableAlpha(true)

                        .showValue(true);
                ColorPickerPopup build = builder.build();

                build.show(new ColorPickerPopup.ColorPickerObserver() {

                    @RequiresApi(api = Build.VERSION_CODES.O)
                    @Override
                    public void onColorPicked(int color) {

                        int[] rgb = util.hexToRGB(color);

                        builder.initialColor(color);

                        Log.e("k" , "r === >"+rgb[0]  +"g===>"+rgb[1]+"b===>"+rgb[2]);
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                OkHttpClient okHttpClient = new OkHttpClient();
                                Request.Builder builder = new Request.Builder()
                                        .url("http:192.168.4.1/colorMode?r="+rgb[0]+"&g="+rgb[1]+"&b="+rgb[2])
                                        .get();
                                Request request = builder.build();
                                Call call = okHttpClient.newCall(request);
                                try {
                                    String string = call.execute().body().string();
                                    Log.e("t" , string);
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }

                            }
                        }).start();
                    }

                    @Override
                    public void onColor(int color, boolean fromUser) {

                    }
                });
            }
        });
    }

    // 处理 流光溢彩功能
    private void BrilliantEvent(){
        brilliant.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if(!startBrillant){
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            OkHttpClient okHttpClient = new OkHttpClient();
                            Request.Builder builder = new Request.Builder()
                                    .url("http:192.168.4.1/Brilliant")
                                    .get();
                            Request request = builder.build();
                            Call call = okHttpClient.newCall(request);
                            try {
                                String string = call.execute().body().string();
                                Log.e("t" , string);
                            } catch (IOException e) {
                                e.printStackTrace();
                            }

                        }
                    }).start();

                }else {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            OkHttpClient okHttpClient = new OkHttpClient();
                            Request.Builder builder = new Request.Builder()
                                    .url("http:192.168.4.1/closeBrilliant")
                                    .get();
                            Request request = builder.build();
                            Call call = okHttpClient.newCall(request);
                            try {
                                String string = call.execute().body().string();
                                Log.e("t" , string);
                            } catch (IOException e) {
                                e.printStackTrace();
                            }

                        }
                    }).start();
                }
                startBrillant = !startBrillant ;
            }
        });

    }

}